package com.kid.keep.dao.child;

import com.kid.keep.po.Child;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description Child dao
 * @Author JOHN
 * @Create 2019-01-10
 * @Since 1.0.0
 */
@Repository
public class ChildRepository extends ServiceImpl<IChildMapper, Child> implements IChildRepository {

    @Override
    public Child findByParentIdAndNameAndDataStatus(Long parentId, String name, Integer dataStatus) {
        return this.baseMapper.selectOne(new QueryWrapper<Child>().lambda().eq(Child::getParentId, parentId).eq(Child::getName, name).eq(Child::getDataStatus, dataStatus));
    }

    @Override
    public Child findByIdAndParentIdAndDataStatus(Long id, Long userId, Integer dataStatus) {
        return this.baseMapper.selectOne(new QueryWrapper<Child>().lambda().eq(Child::getId, id).eq(Child::getParentId, userId).eq(Child::getDataStatus, dataStatus));

    }

    @Override
    public Child findById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<Child> findAllChildByParentIdAndDataStatus(Long parentId, Integer dataStatus) {
        return this.baseMapper.selectList(new QueryWrapper<Child>().lambda().eq(Child::getParentId, parentId).eq(Child::getDataStatus, dataStatus).orderByDesc(Child::getUpdateDate));
    }

    @Override
    public int updateByIdAndParentId(Child child, Long id, Long parentId) {
        return this.baseMapper.update(child, new UpdateWrapper<Child>().lambda().eq(Child::getParentId, parentId).eq(Child::getId, id));
    }

    @Override
    public int updateByIdAndDataStatus(Child child, Long id, Integer dataStatus) {
        return this.baseMapper.update(child, new UpdateWrapper<Child>().lambda().eq(Child::getId, id).eq(Child::getDataStatus, dataStatus));
    }

    @Override
    public Child findNewestChildByParentIdAndDataStatus(Long parentId, Integer dataStatus) {
        return this.baseMapper.findNewestChildByParentIdAndDataStatus(parentId, dataStatus);
    }


}


